package mqpaydemo.paydemo.mq;

import mqpaydemo.paydemo.pay.request.OrdersRequest;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @Description:确认发送
 * @Author: liujinsheng
 * @Date: 2021/9/28.
 */
@Component
public class ConfirmSend {
    @Resource
    RabbitTemplate rabbitTemplate;

    // 配置 confirm 机制
  private final    RabbitTemplate.ConfirmCallback confirmCallback=new RabbitTemplate.ConfirmCallback() {
        @Override
        public void confirm(CorrelationData correlationData, boolean b, String s) {
            if (b) {
                System.out.println("confirm 消息确认成功..." + correlationData.getId());
            } else {
                System.out.println("confirm 消息确认失败..." + correlationData.getId() + " cause: " + s);
            }
        }
    };
    public void confirmSend(){
        OrdersRequest ordersRequest=new OrdersRequest();
        ordersRequest.setUserId(123L);
        rabbitTemplate.convertAndSend("queue_confirm",ordersRequest,new CorrelationData(""+ LocalDateTime.now().toString()));
        rabbitTemplate.setConfirmCallback(confirmCallback);
    }
}
